package com.jar.springbootsecuritydemo.config;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * jwt 认证进入点 【认证失败处理类】
 * 这里根据业务返回自己的Response类
 */
@Component
@Slf4j
public class JwtAuthenticationHandle implements AuthenticationEntryPoint, Serializable {

    private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException exception) throws IOException {
        String msg = StringUtils.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
        response.setStatus(200);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        Map<String, Object> result = new HashMap<>(3);
        result.put("code", 401);
        result.put("msg", msg);
        result.put("data", "token无效或过期,请重新登录");
        response.getWriter().write(JSONObject.toJSONString(result));

        /*
        下面的可以使用全局拦截器进行返回，或者在具体的controller中进行返回
         */
//        response.setStatus(200);
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//        //这些对于操作的处理类可以根据不同异常进行不同处理
//        if (exception instanceof UsernameNotFoundException) {
//            log.info("【登录失败】" + exception.getMessage());
//            Map<String, Object> map = new HashMap<>(2);
//            map.put("code", 500);
//            map.put("msg", exception.getMessage());
//            response.getWriter().write(JSONObject.toJSONString(map));
//        } else if (exception instanceof LockedException) {
//            log.info("【登陆失败】" + exception.getMessage());
//            Map<String, Object> map = new HashMap<>(2);
//            map.put("code", 500);
//            map.put("msg", "用户被冻结");
//            response.getWriter().write(JSONObject.toJSONString(map));
//        } else if (exception instanceof BadCredentialsException) {
//            log.info("【登陆失败】" + exception.getMessage());
//            Map<String, Object> map = new HashMap<>(2);
//            map.put("code", 500);
//            map.put("msg", exception.getMessage());
//            response.getWriter().write(JSONObject.toJSONString(map));
//        } else {
//            Map<String, Object> map = new HashMap<>(2);
//            map.put("code", 500);
//            map.put("msg", "登录失败");
//            response.getWriter().write(JSONObject.toJSONString(map));
//        }
    }
}

